// SPDX-License-Identifier: GPL-2.0
/*
 * SCHEDULE: HUNG TASK
 *
 *  - KERNEL: ENABLE CONFIG_DETECT_HUNG_TASK
 *
 * (C) 2024.05.19 BiscuitOS <buddy.zhang@aliyun.com>
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/sched/signal.h>

static struct task_struct *kthread;

static int hung_task_fn(void *data)
{
	while (!kthread_should_stop()) {
		/* EMULATE RUNNING LOOGTIME */
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(HZ * 200); /* 200s HUNGTASK */

		if (fatal_signal_pending(current)) {
			printk("KTHREAD RECV FATAL SIGNAL.\n");
			break;
		}

		printk("HUNG TASK KTHREAD RUNNING.\n");
	}

	return 0;
}

static int __init BiscuitOS_init(void)
{
	kthread = kthread_run(hung_task_fn, NULL, "BiscuitOS-HungTask");
	if (!kthread)
		return -EINVAL;

	return 0;
}

static void __exit BiscuitOS_exit(void) { }

module_init(BiscuitOS_init);
module_exit(BiscuitOS_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("BiscuitOS <buddy.zhang@aliyun.com>");
MODULE_DESCRIPTION("BiscuitOS OOPS");
